package com.zztl.infiniteBand.ui.activity


import android.graphics.Paint
import android.os.CountDownTimer
import android.support.v4.view.animation.LinearOutSlowInInterpolator
import android.view.View
import butterknife.OnClick
import com.githang.statusbar.StatusBarCompat

import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BaseMvpActivity
import com.zztl.infiniteBand.eventbus.UpdateForceVlueEvent
import com.zztl.infiniteBand.presenter.BloodpressurePresenter
import com.zztl.infiniteBand.ui.view.mvpview.BloodpressureView
import kotlinx.android.synthetic.main.activity_measure_bloodpressure.*
import org.greenrobot.eventbus.EventBus


class MeasureBloodpressureActivity : BaseMvpActivity<BloodpressureView, BloodpressurePresenter>(), BloodpressureView {

    private lateinit var mPresenter: BloodpressurePresenter

    override fun createPresenter(): BloodpressurePresenter {
        mPresenter = BloodpressurePresenter()
        return mPresenter
    }


    lateinit var mCountDownTimer: CountDownTimer
    var timeOut = 30

//    var heartInfo: HeartInfo? = null

    override fun setLayoutId(): Int = R.layout.activity_measure_bloodpressure

    override fun initBase() {
        StatusBarCompat.setTranslucent(window, true)
        mCountDownTimer = object : CountDownTimer(30 * 1000, 1000) {
            override fun onFinish() {
                stopMeasure()
            }

            override fun onTick(millisUntilFinished: Long) {
                tvTime.text = getString(R.string.测量倒计时) + "${--timeOut}S"
            }

        }

        tvTime.text = getString(R.string.测量倒计时) + "${timeOut}S"

//        BraceletApplication.zhBraceletService?.addSimplePerformerListenerLis(mPerformerListener)

        ivAnim?.let {
            it.mDuration = 5000
            it.setStyle(Paint.Style.FILL)
            it.setColor(resources.getColor(R.color.color_007aff))
            it.interpolator = LinearOutSlowInInterpolator()
            it.mInitialRadius = 120f
            it.mMaxRadiusRate = 1f
        }
    }


    @OnClick(R.id.btnReturn, R.id.btnMeasure)
    fun onClick(view: View) {
        when (view.id) {
            R.id.btnReturn -> finish()
            R.id.btnMeasure -> {
             /*   BraceletApplication.zhBraceletService?.bleConnectState?.let {
                    if (it) {
                        btnMeasure.isClickable = false
                        BraceletApplication.zhBraceletService?.openMeasurement()
                        ivAnim.start()
                        mCountDownTimer.start()
                        measure.text = getString(R.string.测量中)
//                        waveView.startAnimation()
                        waveView.setStart(true)

                    } else {
                        toast(getString(R.string.设备未连接))
                    }
                }
*/
            }
        }
    }

    private fun stopMeasure() {
        timeOut = 30
        tvTime.text = getString(R.string.测量倒计时) + "${timeOut}S"
//        BraceletApplication.zhBraceletService?.closeMeasurement()
        measure.text = getString(R.string.测量)
        btnMeasure.isClickable = true
        ivAnim.stop()
//        waveView.stopAnimation()
        waveView.stop()
     /*   heartInfo?.let {
            BillPreference.putApp(PreferenceKeys.BP.name, "${it.heartInfoSBP}/${it.heartInfoDBP}")
            mPresenter.uploadData(UploadBloodpressureRequest("${it.heartInfoSBP}/${it.heartInfoDBP}", it.heartInfoHR.toString()), this)
        }*/
    }

    override fun success() {
        EventBus.getDefault().post(UpdateForceVlueEvent())
    }

/*
    private val mPerformerListener = object : SimplePerformerListener() {


        override fun onResponseHeartInfo(mHeartInfo: HeartInfo?) {
            runOnUiThread {
                mHeartInfo?.let {
                    heartInfo = it
                    tvHeartrate.text = "${it.heartInfoHR}bmp"
                    tvBloodpressure.text = "${it.heartInfoSBP}/${it.heartInfoDBP}"

                }
            }

        }

    }*/


    override fun onDestroy() {

//        waveView.stopAnimation()
        waveView.stop()
        mCountDownTimer.cancel()
        super.onDestroy()
    }
}
